package com.xd.study.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.xd.study.R;

import org.w3c.dom.Text;

/**
 * Created by DGF on 2015/4/22.
 */
public class ProgressBarActivity extends Activity {

    private TextView txtView;
    private ProgressBar progressHoriz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //启用窗口特征，启用带进度条和不带进度的进度条
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        setContentView(R.layout.progressbar);

        setTitle("测试进度条");

        //显示两种进度条
        setProgressBarVisibility(true);
        setProgressBarIndeterminateVisibility(true);

        //Max=10000
        setProgress(5000);

        //设置第一进度条的进度
//        progressBar5.setProgress(int);
        //设置第二进度条的进度
//        progressBar5.setSecondaryProgress(int);
//        progressBar5.getProgress();
//        progressBar5.getSecondaryProgress();
        //增加第一进度条
//        progressBar5.incrementProgressBy(int);
        //增加第二进度条
//        progressBar5.incrementSecondaryProgressBy(int);
        //获取最大进度
//        progressBar5.getMax();

        progressHoriz = (ProgressBar)this.findViewById(R.id.progress_horiz);
        Button btnIncrement = (Button)this.findViewById(R.id.progress_increment);
        Button btnReduce = (Button)this.findViewById(R.id.progress_reduce);
        Button btnReset = (Button)this.findViewById(R.id.progress_reset);
        txtView = (TextView)this.findViewById(R.id.progress_show);

        //显示进度条的进度
        updateProgress();

        btnIncrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressHoriz.incrementProgressBy(5);
                progressHoriz.incrementSecondaryProgressBy(8);
                updateProgress();
            }
        });

        btnReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressHoriz.incrementProgressBy(-5);
                progressHoriz.incrementSecondaryProgressBy(-5);
                updateProgress();
            }
        });

        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressHoriz.setProgress(50);
                progressHoriz.setSecondaryProgress(80);
                updateProgress();
            }
        });

        //显示进度条对话框
        Button btnShowDialog = (Button)this.findViewById(R.id.progress_dialog_show);
        btnShowDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog progressDialog = new ProgressDialog(ProgressBarActivity.this);
                //设置样式
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //设置文本及图标
                progressDialog.setTitle("慕课网");
                progressDialog.setMessage("欢迎欢迎欢迎欢迎");
                progressDialog.setIcon(R.drawable.item);
                //设置进度信息
                progressDialog.setMax(100);
                progressDialog.incrementProgressBy(50);
                progressDialog.setIndeterminate(false);//精确显示
                //设置按钮
                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(ProgressBarActivity.this, "欢迎支持慕课网", Toast.LENGTH_SHORT).show();
                    }
                });
                //设置是否可以通过返回键退出对话框
                progressDialog.setCancelable(true);
                //显示对话框
                progressDialog.show();
            }
        });
    }

    private void updateProgress(){
        int max = progressHoriz.getMax();
        int first = progressHoriz.getProgress();
        int second = progressHoriz.getSecondaryProgress();

        txtView.setText("第一进度百分比："+(int)(first/(float)max*100)+"% 第二进度百分比："+(int)(second/(float)max*100)+"%");
    }
}
